package com.yztc.youbadbad.ui.homepage;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.yztc.youbadbad.App;
import com.yztc.youbadbad.R;
import com.yztc.youbadbad.adapter.ProdectRecyclerAdapter;
import com.yztc.youbadbad.adapter.TuijianHomepageRecyclerAdapter;
import com.yztc.youbadbad.base.BaseFragment;
import com.yztc.youbadbad.bean.homepage.ProductBean;
import com.yztc.youbadbad.bean.homepage.ZhuanchangBean;
import com.yztc.youbadbad.bean.homepage.ZhuanchangListBean;
import com.yztc.youbadbad.helper.AssetHelper;
import com.yztc.youbadbad.helper.DividerItemDecoration;
import com.yztc.youbadbad.utils.UIManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 首页 推荐
 */
public class TuijianHomeFragment extends BaseFragment
        implements ProdectRecyclerAdapter.IProdectClickListener{
    @BindView(R.id.recyclerview_tuijian)
    RecyclerView recyclerviewTuijian;

    private Context mContext = App.app;//获取不到context，出此下策
    private List<ZhuanchangBean> mTuijianList = new ArrayList<>();
    private TuijianHomepageRecyclerAdapter mAdapter = null;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tuijian_home, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
        initView();
    }

    //recyclerView
    private void initView() {
        recyclerviewTuijian.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST));
        LinearLayoutManager manager = new LinearLayoutManager(mContext);

        mAdapter = new TuijianHomepageRecyclerAdapter(mContext, mTuijianList, this);
        recyclerviewTuijian.setAdapter(mAdapter);
        recyclerviewTuijian.setLayoutManager(manager);
    }


    //初始数据 从Asset下读取
    private void initData() {
        String result = AssetHelper.getFromAssets(getActivity(), "indexUrl.txt");//读取推荐

        try {
            JSONObject jsonObject = new JSONObject(result);
            JSONArray data = jsonObject.getJSONArray("data");
            for (int i = 0; i < data.length(); i++) {//因data美容不同手动解析
                getBeans(i, data);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    /**
     *
     * @param i 下标
     * @param data JsonArray
     * @throws JSONException
     */
    private void getBeans(int i, JSONArray data) throws JSONException {
        JSONObject bean = data.getJSONObject(i);
        String str = bean.toString();
        Gson gson = new Gson();
        switch (i){
            case 0://轮播
                break;
            case 1://热门消息
                break;
            case 2://功能入口模块
                break;
            case 3://秒杀专场
                break;
            case 4://固定常规
                break;
            case 5://强力推荐
                break;
            case 6://格子直播
                break;
            case 7://超级导购
                break;
            case 8://好物推荐
                break;
            case 9://新品推荐
                break;
            case 10://专场列表

                ZhuanchangListBean list10 = gson.fromJson(str, ZhuanchangListBean.class);
                mTuijianList.addAll(list10.getData().getList());
                break;
            case 11://格子推荐
                break;
            case 12://{"last_update_time":1479197337}更新时间
                break;
        }
    }

    @Override
    public void producetItemclicked(int position, int index2) {
        ProductBean productBean = mTuijianList.get(position).getEvent_product().get(index2);
        UIManager.startProduct_DetialInfo(getParentFragment().getActivity(), productBean);//跳转商品详情画面
    }
}
